/*
GANESHA - Reasoning on critical infrastructures affected by emergency situations
Copyright (c) 2010-2011, Information Eng. Research Unit - Univ. of Alcala
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package es.uah.cc.ie.ganesha.messaging;

import es.uah.cc.ie.ganesha.messaging.position.MessagePosition;
import java.util.ArrayList;
import java.util.List;

/**
 * Represents the interface of an alert message. Actual implementations follow
 * different specifications, e.g. CAP or TSO.
 *
 * @author ieru
 */
public class AlertMessage {

	protected String _identifier;
	protected boolean _isUnique;
	protected List<MessagePosition> _positions;
	protected String _eventType;
	protected String _node;
	protected String _domain;
	protected int _priority;
	protected List<AlertMessage> _otherMessages;

	/**
	 * Constructor
	 */
	public AlertMessage() {
		_positions = new ArrayList<MessagePosition>();
	}

	/**
	 * Constructor
	 * @param identifier Message identifier.
	 */
	public AlertMessage(String identifier) {
		this();

		this._identifier = identifier;
	}

	/**
	 * Returns message's domain
	 * @return Domain.
	 */
	public String getDomain() {
		return _domain;
	}

	/**
	 * Sets message's domain
	 * @param _domain Domain
	 */
	public void setDomain(String _domain) {
		this._domain = _domain;
	}

	/**
	 * Returns node's name
	 * @return Node's name
	 */
	public String getNodeName() {
		return _node;
	}

	/**
	 * Sets node's name.
	 * @param _node Node's name.
	 */
	public void setNodeName(String _node) {
		this._node = _node;
	}

	/**
	 * Returns priority.
	 * @return Priority.
	 */
	public int getPriority() {
		return _priority;
	}

	/**
	 * Sets priority.
	 * @param _priority Priority
	 */
	public void setPriority(int _priority) {
		this._priority = _priority;
	}

	/**
	 * Returns message's identifier.
	 * @return Identifier
	 */
	public String getIdentifier() {
		return _identifier;
	}

	/**
	 * Sets the submessages of an unique message (which groups other messages).
	 * @param messages Submessages.
	 */
	public void setUniqueSubMessages(List<AlertMessage> messages) {
		_isUnique = true;

		_otherMessages = messages;
	}

	/**
	 * Returns whether or not this message is unique (groups others messages)
	 * @return true if it's unique, false if not
	 */
	public boolean isUnique() {
		return _isUnique;
	}

	/**
	 * Returns submessages in case this is a unique message (groups other messages)
	 * @return Submessages
	 */
	public List<AlertMessage> getUniqueSubMessages() {
		return _otherMessages;
	}

	/**
	 * Returns event type
	 * @return Event type
	 */
	public String getEventType() {
		return _eventType;
	}

	/**
	 * Sets event type
	 * @param _eventType Event type
	 */
	public void setEventType(String _eventType) {
		this._eventType = _eventType;
	}

	/**
	 * Returns the incident's center latitude.
	 * @return Incident's center latitude.
	 */
	public double getCenterLatitude()
	{
		if(_positions.size() == 0)
			return 0;

		double result = 0;

		for(MessagePosition pos : _positions)
			result += pos.getCenterLatitude();

		result /= (double) _positions.size();

		return result;
	}

	/**
	 * Returns the incident's center longitude.
	 * @return Incident's center longitude.
	 */
	public double getCenterLongitude()
	{
		if(_positions.size() == 0)
			return 0;

		double result = 0;

		for(MessagePosition pos : _positions)
			result += pos.getCenterLongitude();

		result /= (double) _positions.size();

		return result;
	}

	/**
	 * Returns Positions
	 * @return Positions
	 */
	public List<MessagePosition> getPositions() {
		return _positions;
	}

	/**
	 * Add a position
	 * @param position Position
	 */
	public void addPosition(MessagePosition position) {
		this._positions.add(position);
	}
}
